using System;
using System.ComponentModel;
using Chambills.Present.Core;
using Chambills.Present.Core.NotifyOnChange;

namespace Chambills.Present.Validation.Sample.WPF
{
    public class ValidatingViewModel : NotifiableObject, IValidatingViewModel
    {
        public ValidatingViewModel()
        {
            Validator = new DefaultViewModelValidator(new AttributeBasedViewModelValidatorContributor()) {Model = this};
            
            //We enable validation after a property changes
            // so the whole screen isn't red to start with.
            PropertyChanged += EnableValidation;
        }

        private void EnableValidation(object sender, PropertyChangedEventArgs e)
        {
            Validator.Enabled = true;
            PropertyChanged -= EnableValidation;

        }


        public bool IsValid
        {
            get { return Validator.IsValid; }
        }

        public string[] Errors
        {
            get { return Validator.GetCurrentErrors().ToArray(); }
        }

        public string Error
        {
            get { return Validator.Error; }
        }

        public DefaultViewModelValidator Validator { get; set; }

        public string this[string columnName]
        {
            get { return Validator[columnName]; }
        }
    }
}